var global_Art_Var = {//文章、评论、公告、友链共用
    pageNum: 1,//分页：当前页
    pageSize: 10,//分页：每页加载内容的大小
    totalPage: 0,//最大页
    expireTime: 1000 * 60 * 60 * 24 * 7,//设置本地存储过期时间,默认一个周
    isLoadPagination: true//是否刷新分页按钮，true：刷新，false：不刷心
}
var setting = {
    data: {
        key: {
            name: "menu_name"
        },
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parent_id",
            rootPId: null
        }
    }, callback: {
        onClick: zTreeOnClick
    }
};
var dropdownMenuTime = null;

function initjeDate() {
    return {
        isinitVal: false,//设置默认时间
        festival: false,//设置农历
        format: 'YYYY-MM-DD hh:mm:ss'
    }
}

function dropdownMenuClick() {
    var data = $(this).attr("data");
    var text = $(this).text();
    $(this).parents("div.input-group").find("input.form-control").val(text).attr("data", data).focus();
    $(this).parents("ul.dropdown-menu").slideUp();

}

function loadNavBarTree() {
    $.ajax({
        url: "/navbar/back/getNavBarToZtree.zb",
        type: "POST",
        dataType: "JSON",
        success: function (data) {
            if (data && data.length > 0) {
                $.fn.zTree.init($("#ztree"), setting, data);
            } else {
                layer.msg("获取菜单信息失败！");
            }
        }, error: function () {
            layer.msg("获取菜单信息失败！");
        }

    })
}

function zTreeOnClick(event, treeId, treeNode) {
    if (treeNode.isParent) return false;
    var text = treeNode.name;
    var id = treeNode.id;
    $("#menu").val(text).data("id", id);
    $("#menuTree").slideUp();
}

function zTreeOnClick(event, treeId, treeNode) {
    var text = "";
    if (treeNode.level === 0 && !treeNode.children) {//第一层且没有子节点
        text = treeNode.menu_name;
    } else if (treeNode.level === 0 && treeNode.children) {
        return false;
    } else if (treeNode.level === 1) {//第二层
        text = treeNode.getParentNode().menu_name + "  >  " + treeNode.menu_name;
    }
    var id = treeNode.id;
    $("#menu").val(text).data("id", id).focus();
    $("#menuTree").slideUp();
}

function ztreeBtnClick() {
    $(this).next().slideToggle();
}

function dropdownMenuMouseEnter() {
    if (dropdownMenuTime) {
        clearTimeout(dropdownMenuTime);
    }
}

function dropdownMenuMouseLeave() {
    var $this = $(this);
    dropdownMenuTime = setTimeout(function () {
        $this.slideUp();
    }, 500)
}

function operateBtnClick() {
    $(this).next("ul").slideDown();
}

function optUlMouseleave() {
    $(this).slideUp();
}

function tableFilterClick() {
    $(this).parents(".panel").find(".panel-body").slideToggle();
    $(this).next("a.table-reset").toggle();
}

function resetDataClick() {
    $(this).val("");
    $(this).removeAttr("data");
    $(this).removeData();
}

//这里文章、评论、公告、友链共用一套分页事件
function pagingEvent(e) {
    var name = e.data.name;
    var curPageNum = global_Art_Var.pageNum;
    var $siblings = $(this).siblings();
    var $lis = $(this).parent().find("li");
    if ($(this).hasClass("page-prev")) {
        if ($(this).hasClass("disabled")) return false;
        var $activeLi = $siblings.filter(".active");
        var $prevLi = $activeLi.prev();
        $prevLi.addClass("active")
        $activeLi.removeClass("active");
        global_Art_Var.pageNum--;
    } else if ($(this).hasClass("page-next")) {
        if ($(this).hasClass("disabled")) return false;
        var $activeLi = $siblings.filter(".active");
        var $nextLi = $activeLi.next();
        $nextLi.addClass("active")
        $activeLi.removeClass("active");
        global_Art_Var.pageNum++;
    } else {
        $siblings.removeClass("active");
        $(this).addClass("active");
        global_Art_Var.pageNum = $(this).text() - 0;
    }
    var $nextBtn = $("ul.pagination li.page-next");
    var $prevBtn = $("ul.pagination li.page-prev");
    if (global_Art_Var.totalPage === 1) {
        $nextBtn.addClass("disabled");
        $prevBtn.addClass("disabled");
    } else if (global_Art_Var.pageNum >= global_Art_Var.totalPage) {
        $nextBtn.addClass("disabled");
        $prevBtn.removeClass("disabled")
    } else if (global_Art_Var.pageNum <= 1) {
        $prevBtn.addClass("disabled");
        $nextBtn.removeClass("disabled");
    } else {
        $nextBtn.removeClass("disabled");
        $prevBtn.removeClass("disabled");
    }
    if (curPageNum != global_Art_Var.pageNum) {
        if (name == "article") {
            loadArticleList(false);
        } else if (name == "message") {
            loadMessageList(false);
        } else if (name == "notice") {
            loadNoticeList(false);
        } else if (name == "friendLink") {
            loadFriendLinkList(false)
        }
    }
}

//格式化过长的内容
function formatCont(cont, len) {
    if (cont && cont.length > len) {
        var newCont = "";
        return cont.substring(0, len) + "......";
    } else {
        return cont;
    }
}

//格式化空值
function fmtEmpty(value) {
    if (!value) {
        return "";
    } else {
        return value;
    }
}

function fn() {
}

$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};


